from typing import List


class Solution:
    def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
        ans = []
        for word in words:
            if self.match(word, pattern) and self.match(pattern, word):
                ans.append(word)
        return ans

    def match(self, word, pattern):
        d = {}
        for i, c in enumerate(word):
            if c not in d:
                d[c] = pattern[i]
            if d[c] != pattern[i]:
                return False
        return True